﻿/**
 * jQuery EasyUI 1.4
 * 
 * Copyright (c) 2009-2014 www.jeasyui.com. All rights reserved.
 * 
 * Licensed under the GPL license: http://www.gnu.org/licenses/gpl.txt To use it
 * on other terms please contact us at info@jeasyui.com
 * 
 */
(function($) {
	function createButcombo(target) {
		var butcombo = $.data(target, "butcombo");
		var opts = butcombo.options;
		if (!butcombo.panel) {
			if(opts.isBody){
				butcombo.panel = $("<div class=\"butcombo-panel\"></div>").appendTo("body");
			}else{
				var tablewrapCombo=$(target).closest("div.datagrid-top-wrap");
				if(tablewrapCombo.length<=0){
					$(target).data("tablewrapCombo",null);
					butcombo.panel = $("<div class=\"butcombo-panel\"></div>").appendTo($(target).wrap('<div class="easyui-top-wrap"></div>').parent());
				}else{
					$(target).data("tablewrapCombo",tablewrapCombo);
					butcombo.panel = $("<div class=\"butcombo-panel\"></div>").appendTo(tablewrapCombo);
				}
			}
			butcombo.panel.panel({
				minWidth : opts.panelMinWidth,
				maxWidth : opts.panelMaxWidth,
				minHeight : opts.panelMinHeight,
				maxHeight : opts.panelMaxHeight,
				doSize : false,
				closed : true,
				cls : "butcombo-p",
				method:opts.method,
				queryParams:opts.queryParams,
				href:opts.href,
				iframe:opts.iframe,
				style : {
					position : "absolute",
					zIndex : 10
				},
				onOpen : function() {
					var p = $(this).panel("panel");
					if ($.fn.menu) {
						p.css("z-index", $.fn.menu.defaults.zIndex++);
					} else {
						if ($.fn.window) {
							p.css("z-index", $.fn.window.defaults.zIndex++);
						}
					}
					$(this).panel("resize");
				},
				onBeforeClose : function() {
				},
				onClose : function() {
					var butcombo = $.data(target, "butcombo");
					if (butcombo) {
						butcombo.options.onHidePanel.call(target);
					}
				}
			});
		}
		$(target).attr("comboName", $(target).attr("textboxName"));
		butcombo.butcombo = $(target);
		butcombo.butcombo.addClass("butcombo");
	};
	function destroy(target) {
		var butcombo = $.data(target, "butcombo");
		butcombo.panel.panel("destroy");
	};
	function comboArrow(target) {
		var panel = $.data(target, "butcombo").panel;
		if (panel.is(":visible")) {
			hidePanel(target);
		} else {
			var opts = $(target).butcombo("options");
			var show=opts.onBeforeShowPanel.call(target);
			if(show){
				var p = $(target).closest("div.butcombo-panel");
				$("div.butcombo-panel:visible").not(panel).not(p).panel("close");
				$(target).butcombo("showPanel");
			}			
		}
	};
	function bindEvent(target) {
		$(target).unbind(".butcombo").bind("mousedown.butcombo",function(e) {
			comboArrow(target);
		});
		$(document).unbind(".butcombo").bind("mousedown.butcombo", function(e) {
			var p = $(e.target).closest("span.butcombo,div.butcombo-p,div.easyui-top-wrap,div.datagrid-top-wrap");
			if (p.length) {
				return;
			}
			$("body>div.butcombo-p>div.butcombo-panel:visible").panel("close");
			$("div.easyui-top-wrap>div.butcombo-p>div.butcombo-panel:visible").panel("close");
			$("div.datagrid-top-wrap>div.butcombo-p>div.butcombo-panel:visible").panel("close");
		});
	};
	function clickEvent(e) {
		var target = e.data.target;
		var butcombo = $.data(target, "butcombo");
		var opts = butcombo.options;
		var panel = butcombo.panel;
		if (!opts.editable) {
			comboArrow(target);
		} else {
			var p = $(target).closest("div.butcombo-panel");
			$("div.butcombo-panel:visible").not(panel).not(p).panel("close");
		}
	};
	function keydownEvent(e) {
		var target = e.data.target;
		var t = $(target);
		var butcombo = t.data("butcombo");
		var opts = t.butcombo("options");
		switch (e.keyCode) {
		case 38:
			opts.keyHandler.up.call(target, e);
			break;
		case 40:
			opts.keyHandler.down.call(target, e);
			break;
		case 37:
			opts.keyHandler.left.call(target, e);
			break;
		case 39:
			opts.keyHandler.right.call(target, e);
			break;
		case 13:
			e.preventDefault();
			opts.keyHandler.enter.call(target, e);
			return false;
		case 9:
		case 27:
			hidePanel(target);
			break;
		}
	};
	function showContent(target,html) {
		var butcombo = $.data(target, "butcombo");
		var panel = butcombo.panel;
		$(html).appendTo(panel);
	}
	
	function showPanel(target) {
		var butcombo = $.data(target, "butcombo");
		var combocb = butcombo.butcombo;
		var panel = butcombo.panel;
		var opts = $(target).butcombo("options");
		panel.panel("move", {
			left : _getLeft(),
			top : _getTop()
		});
		if (panel.panel("options").closed) {
			panel.panel("open").panel("resize", {
				width : (opts.panelWidth ? opts.panelWidth : combocb._outerWidth()),
				height :(opts.panelHeight ? opts.panelHeight : combocb._outerHeight())
			});
			opts.onShowPanel.call(target);
		}
		(function() {
			if (panel.is(":visible")) {
				panel.panel("move", {
					left : _getLeft(),
					top : _getTop()
				});
				setTimeout(arguments.callee, 200);
			}
		})();
		function _getLeft() {
			var tablewrapCombo=$(target).data("tablewrapCombo");
			if(tablewrapCombo){
				var tableLeft=tablewrapCombo.position().left;
				var panelWidth=panel._outerWidth();
				var combocbWidth=combocb._outerWidth();
				var left = combocb.position().left;
				var winWidth=$(window)._outerWidth()+$(document).scrollLeft();
				if (tableLeft+left+panelWidth +38> winWidth) {
					return winWidth - panelWidth - combocbWidth -38;
				}else{
					return left-combocbWidth;
				}
			}else{
				var left = combocb.position().left;
				if (opts.panelAlign == "right") {
					left += combocb._outerWidth() - panel._outerWidth();
				}
				if (left + panel._outerWidth() > $(window)._outerWidth() + $(document).scrollLeft()) {
					left = $(window)._outerWidth() + $(document).scrollLeft() - panel._outerWidth();
				}
				if (left < 0) {
					left = 0;
				}
				return left;
			}
		};
		function _getTop() {
			var tablewrapCombo=$(target).data("tablewrapCombo");
			if(tablewrapCombo){
				var top = 0;
				var tableTop=tablewrapCombo.position().top;
				var pTop = combocb.position().top;
				var combOutH= combocb._outerHeight();
				var winOutH=$(window)._outerHeight();
				var panelOutH= panel._outerHeight();
				var docSTop= $(document).scrollTop();
				if (tableTop + pTop + panelOutH +5 > winOutH+docSTop) {
					return top-panelOutH+pTop-5;
				}else{
					return top+pTop+combOutH+5;
				}
			}else{
				var top = combocb.position().top + combocb._outerHeight();
				if (top + panel._outerHeight() > $(window)._outerHeight() + $(document).scrollTop()) {
					top = combocb.offset().top - panel._outerHeight();
				}
				if (top < $(document).scrollTop()) {
					top = combocb.offset().top + combocb._outerHeight();
				}
				return top;
			}
			
		};
	};
	function hidePanel(target) {
		var panel = $.data(target, "butcombo").panel;
		panel.panel("close");
	};
	
	$.fn.butcombo = function(options, param) {
		if (typeof options == "string") {
			var method = $.fn.butcombo.methods[options];
			if (method) {
				return method(this, param);
			} else {
				return this.linkbutton(options, param);
			}
		}
		options = options || {};
		return this.each(function() {
			var butcombo = $.data(this, "butcombo");
			if (butcombo) {
				$.extend(butcombo.options, options);
				if (options.value != undefined) {
					butcombo.options.originalValue = options.value;
				}
			} else {
				butcombo = $.data(this, "butcombo", {
					options : $.extend({}, $.fn.butcombo.defaults, $.fn.butcombo.parseOptions(this), options),
					previousText : ""
				});
				butcombo.options.originalValue = butcombo.options.value;
			}
			createButcombo(this);
			bindEvent(this);
			if(butcombo.options.content){
				showContent(this,butcombo.options.content);
			}
		});
	};
	$.fn.butcombo.methods = {
		options : function(jq) {
			return $.data(jq[0], "butcombo").options;
		},
		panel : function(jq) {
			return $.data(jq[0], "butcombo").panel;
		},
		content : function(jq,html) {
			return jq.each(function() {
				showContent(this,html);
			});
		},
		destroy : function(jq) {
			return jq.each(function() {
				destroy(this);
			});
		},
		showPanel : function(jq) {
			return jq.each(function() {
				showPanel(this);
			});
		},
		hidePanel : function(jq) {
			return jq.each(function() {
				hidePanel(this);
			});
		}
	};
	$.fn.butcombo.parseOptions = function(target) {
		var t = $(target);
		return $.extend({}, $.parser.parseOptions(target, [  "panelAlign", {
					panelWidth : "number"
				}, {
					panelMinWidth : "number",
					panelMaxWidth : "number",
					panelMinHeight : "number",
					panelMaxHeight : "number"
				} ]), {
			panelHeight : (t.attr("panelHeight") == "auto" ? "auto" : parseInt(t.attr("panelHeight")) || undefined)
		});
	};
	$.fn.butcombo.defaults = $.extend({}, {
		inputEvents : {
			click : clickEvent,
			keydown : keydownEvent,
			paste : keydownEvent,
			drop : keydownEvent
		},
		isBody:false,
		panelWidth : 500,
		panelHeight : 260,
		panelMinWidth : null,
		panelMaxWidth : null,
		panelMinHeight : null,
		panelMaxHeight : null,
		queryParams:null,
		href:null,
		iframe:null,
		content:null,
		method:"get",
		panelAlign : "right",
		keyHandler : {
			up : function(e) {
			},
			down : function(e) {
			},
			left : function(e) {
			},
			right : function(e) {
			},
			enter : function(e) {
			},
			query : function(q, e) {
			}
		},
		onBeforeShowPanel : function() {
			return true;
		},
		onShowPanel : function() {
		},
		onHidePanel : function() {
		},
		onChange : function(_52, _53) {
		}
	});
})(jQuery);
